package com.wit.WEB.controller;

import com.github.pagehelper.PageInfo;
import com.wit.WEB.entity.Order_;
import com.wit.WEB.entity.Result;
import com.wit.WEB.entity.TestStand;
import com.wit.WEB.entity.User;
import com.wit.WEB.mapper.TestStandMapper;
import com.wit.WEB.service.OrderService;
import com.wit.WEB.service.ResultService;
import com.wit.WEB.service.TestStandService;
import com.wit.WEB.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @program: java-web-group
 * @description:检测站控制器
 * @author: KingTeng
 * @create: 2022-02-28 14:44
 **/
@Controller
public class TestStationController {
    @Autowired
    private TestStandService testStandService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ResultService resultService;
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") int page,
                                @RequestParam(name = "size",required = true,defaultValue = "2") int size) throws Exception {
        ModelAndView mv=new ModelAndView();
        List<TestStand> testStandList = testStandService.list(page,size);
        //PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(testStandList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("forward:/foreUser");
        return mv;
    }



    @RequestMapping("/TestDealed")
    public String dealed(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        Integer uid=user.getId();
        TestStand testStand=testStandService.findbyUid(uid);
        Integer tid=testStand.getId();
        List<Order_> orders=orderService.findallByConfirmOk(tid);
        session.setAttribute("orders", orders);
        return "/station/listDealed";
    }

    @RequestMapping("/TestdealWith")
    public String dealw(@RequestParam(value = "oid",required = true) String oid, HttpSession session){
        session.setAttribute("oid", oid);
        Order_ order =orderService.selectOrderbyprimary(Integer.parseInt(oid));
        TestStand testStand=testStandService.selectByprimary(Integer.parseInt(order.getAddress()));
        User user= userService.selectByPrimary(order.getUid());
        Result result= resultService.selectByOid(oid);
        System.out.println(order);
        System.out.println(user);
        System.out.println(testStand);
        session.setAttribute("order", order);
        session.setAttribute("testStand",testStand);
        session.setAttribute("result", result);
        session.setAttribute("orderUser", user);
        return "/station/OrderDetail";
    }

    @PostMapping("/TestUpload")
    public String UploadT(String confirmResult, HttpSession session) {
        Order_ order= (Order_) session.getAttribute("order");
        order.setStatus(orderService.confirmed);
        Date date=new Date();
        order.setConfirmdate(date);
        orderService.updateByprimary(order);

        Result result=new Result();
        result.setUid(order.getUid());
        result.setOid(order.getId());
        result.setConfirmdate(date);
        result.setConfirmresult(confirmResult);
        resultService.insertResult(result);
        System.out.println(order);
        return "/station/listOrder";
    }

}
